package com.tanqidi.arcgisandroid.base.utils

import android.opengl.Visibility
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.ViewGroup.LayoutParams.MATCH_PARENT
import android.view.ViewGroup.LayoutParams.WRAP_CONTENT
import android.widget.LinearLayout
import androidx.core.view.setPadding
import com.tanqidi.arcgisandroid.databinding.*

class UIGenerator {

    companion object {

        /**
         * 生成线性布局
         */
        fun buildLinearLayout(
            layoutInflater: LayoutInflater,
            viewGroup: ViewGroup,
            isAttach: Boolean = true,
            myOrientation: Int = LinearLayout.HORIZONTAL,
            myGravity: Int = Gravity.LEFT,
            layoutWeight: Int = -1,
            myMarginLeft: Int = 0,
            myMarginRight: Int = 0,
            myMarginTop: Int = 0,
            myMarginBottom: Int = 0,
            myWidth: Int = MATCH_PARENT,
            myHeight: Int = WRAP_CONTENT,
            myPadding: Int = 0,
            myPaddingLeft: Int = 0,
            myPaddingRight: Int = 0,
            myPaddingTop: Int = 0,
            myPaddingBottom: Int = 0,
            myVisibility: Int = View.VISIBLE,
            myBackgroundColor: Int? = null
        ): ItemLinearlayoutBinding {
            return ItemLinearlayoutBinding.inflate(layoutInflater, viewGroup, isAttach).apply {
                linearLayoutContainer.apply {
                    id = (0..Int.MAX_VALUE).random()
                    visibility = myVisibility
                    orientation = myOrientation
                    gravity = myGravity
                    layoutParams = LinearLayout.LayoutParams(myWidth, myHeight).apply {
                        marginEnd = myMarginRight
                        marginStart = myMarginLeft
                        topMargin = myMarginTop
                        bottomMargin = myMarginBottom

                        if (myBackgroundColor != null) {
                            setBackgroundColor(myBackgroundColor)
                        }

                        if (layoutWeight != -1) {
                            weight = layoutWeight.toFloat()
                        }

                        if (myPadding > 0) {
                            setPadding(myPadding)
                        } else {
                            setPadding(myPaddingLeft, myPaddingTop, myPaddingRight, myPaddingBottom)
                        }
                    }
                }
            }
        }

        /**
         * 输入框
         */
        fun buildEditInputView(
            layoutInflater: LayoutInflater,
            viewGroup: ViewGroup,
            layoutWeight: Int = -1,
            myMarginLeft: Int = 0,
            myMarginRight: Int = 0,
            myMarginTop: Int = 0,
            myMarginBottom: Int = 0,
            myWidth: Int = MATCH_PARENT,
            myHeight: Int = WRAP_CONTENT,
            myPadding: Int = 0,
            myPaddingLeft: Int = 0,
            myPaddingRight: Int = 0,
            myPaddingTop: Int = 0,
            myPaddingBottom: Int = 0,
            isAttach: Boolean = true,
            myGravity: Int? = null,
            myInputType: Int? = -1,
        ) : ItemEditInputBinding {
            return ItemEditInputBinding.inflate(layoutInflater, viewGroup, isAttach).apply {
                textInputEditText.apply {
                    //重新生成一个id给他，不然旋转屏幕都用同一个id，那么数据就错乱了
                    id = (0..Int.MAX_VALUE).random()
                    if (myInputType != null && myInputType != -1) {
                        inputType = myInputType
                    }
                }
                textInputLayout.apply {
                    id = (0..Int.MAX_VALUE).random()
                    layoutParams = LinearLayout.LayoutParams(myWidth, myHeight).apply {
                        marginEnd = myMarginRight
                        marginStart = myMarginLeft
                        topMargin = myMarginTop
                        bottomMargin = myMarginBottom

                        if (layoutWeight != -1) {
                            weight = layoutWeight.toFloat()
                        }
                    }
                }
            }
        }

        /**
         * 文本
         */
        fun buildTextView(
            layoutInflater: LayoutInflater,
            viewGroup: ViewGroup,
            layoutWeight: Int = -1,
            myMarginLeft: Int = 0,
            myMarginRight: Int = 0,
            myMarginTop: Int = 0,
            myMarginBottom: Int = 0,
            myWidth: Int = WRAP_CONTENT,
            myHeight: Int = WRAP_CONTENT,
            myPadding: Int = 0,
            myPaddingLeft: Int = 0,
            myPaddingRight: Int = 0,
            myPaddingTop: Int = 0,
            myPaddingBottom: Int = 0,
            isAttach: Boolean = true,
            myGravity: Int? = null
        ): ItemTextviewBinding {
            return ItemTextviewBinding.inflate(layoutInflater, viewGroup, isAttach).apply {
                textView.apply {
                    id = (0..Int.MAX_VALUE).random()
                    layoutParams = LinearLayout.LayoutParams(myWidth, myHeight).apply {
                        marginEnd = myMarginRight
                        marginStart = myMarginLeft
                        topMargin = myMarginTop
                        bottomMargin = myMarginBottom
                        if (myPadding > 0) {
                            setPadding(myPadding)
                        } else {
                            setPadding(myPaddingLeft, myPaddingTop, myPaddingRight, myPaddingBottom)
                        }

                        if (myGravity != null) {
                            gravity = myGravity
                        }

                        if (layoutWeight != -1) {
                            weight = layoutWeight.toFloat()
                        }
                    }
                }
            }
        }

        fun buildChipView(
            layoutInflater: LayoutInflater,
            viewGroup: ViewGroup,
            isAttach: Boolean = true,
            layoutWeight: Int = -1,
            myMarginLeft: Int = 0,
            myMarginRight: Int = 0,
            myMarginTop: Int = 0,
            myMarginBottom: Int = 0,
            myWidth: Int = WRAP_CONTENT,
            myHeight: Int = WRAP_CONTENT,
        ): ItemChipBinding {
            return ItemChipBinding.inflate(layoutInflater, viewGroup, isAttach).apply {
                chip.apply {
                    id = (0..Int.MAX_VALUE).random()
                    layoutParams = LinearLayout.LayoutParams(myWidth, myHeight).apply {
                        marginEnd = myMarginRight
                        marginStart = myMarginLeft
                        topMargin = myMarginTop
                        bottomMargin = myMarginBottom

                        if (layoutWeight != -1) {
                            weight = layoutWeight.toFloat()
                        }
                    }
                }
            }
        }

        fun buildChipGroupView(
            layoutInflater: LayoutInflater,
            viewGroup: ViewGroup,
            isAttach: Boolean = true,
            layoutWeight: Int = -1,
            myMarginLeft: Int = 0,
            myMarginRight: Int = 0,
            myMarginTop: Int = 0,
            myMarginBottom: Int = 0,
            myWidth: Int = MATCH_PARENT,
            myHeight: Int = WRAP_CONTENT,
        ): ItemChipGroupBinding {
            return ItemChipGroupBinding.inflate(layoutInflater, viewGroup, isAttach).apply {
                chipGroup.apply {
                    id = (0..Int.MAX_VALUE).random()
                    layoutParams = LinearLayout.LayoutParams(myWidth, myHeight).apply {
                        marginEnd = myMarginRight
                        marginStart = myMarginLeft
                        topMargin = myMarginTop
                        bottomMargin = myMarginBottom

                        if (layoutWeight != -1) {
                            weight = layoutWeight.toFloat()
                        }
                    }
                }
            }
        }

        fun buildCardView(
            layoutInflater: LayoutInflater,
            viewGroup: ViewGroup,
            isAttach: Boolean = true,
            layoutWeight: Int = -1,
            myMarginLeft: Int = 0,
            myMarginRight: Int = 0,
            myMarginTop: Int = 0,
            myMarginBottom: Int = 0,
            myWidth: Int = MATCH_PARENT,
            myHeight: Int = WRAP_CONTENT,
        ): ItemCardViewBinding {
            return ItemCardViewBinding.inflate(layoutInflater, viewGroup, isAttach).apply {
                materialCardView.apply {
                    id = (0..Int.MAX_VALUE).random()
                    layoutParams = LinearLayout.LayoutParams(myWidth, myHeight).apply {
                        marginEnd = myMarginRight
                        marginStart = myMarginLeft
                        topMargin = myMarginTop
                        bottomMargin = myMarginBottom

                        if (layoutWeight != -1) {
                            weight = layoutWeight.toFloat()
                        }
                    }
                }
            }
        }


    }
}